package com.lhy.wallet.service.imp;

import com.lhy.wallet.exception.ExceptionEnum;
import com.lhy.wallet.exception.MalletException;
import com.lhy.wallet.mapper.BillMapper;
import com.lhy.wallet.mapper.UserMapper;
import com.lhy.wallet.pojo.Bill;
import com.lhy.wallet.pojo.User;
import com.lhy.wallet.service.UserService;
import com.lhy.wallet.utils.OrderCodeFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: 卢辉宇
 * @date: 2023/3/10 16:22
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    @Resource
    BillMapper billMapper;

    /**
     * 查余额
     * @param userId
     * @return
     * @throws MalletException
     */
    @Override
    public User selectRemainder(Integer userId) throws MalletException {
        User user = userMapper.selectRemainder(userId);
        if (user==null){
            //抛出用户不存在异常
            throw new MalletException(ExceptionEnum.NO_USER);
        }
        return user;
    }

    /**
     * 消费
     * @param userId
     * @param amount
     * @throws MalletException
     */
    @Override
    public void consume(Integer userId, Double amount) throws MalletException {

        User user = userMapper.selectRemainder(userId);
        if (user==null){
            //抛出用户不存在异常
            throw new MalletException(ExceptionEnum.NO_USER);
        }
        //得到消费后的剩余余额
        Double newRemainder=user.getRemainder()-amount;
        if (newRemainder<0){
            //抛出余额不足异常
            throw new MalletException(ExceptionEnum.MONEY_NOT_ENOUGH);
        }

        //更新余额
        userMapper.consume(userId,newRemainder);


        //生成订单id
        String billId= OrderCodeFactory.getOrderCode(userId);
        //装配订单参数
        Bill bill = new Bill();
        bill.setUserId(userId);
        bill.setBillId(billId);
        bill.setRemainder(newRemainder);
        bill.setDetails(amount);
        bill.setRemark("用户消费");
        bill.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        //插入账单
        billMapper.insertConsume(bill);

    }

    /**
     * 退款
     * @param userId
     * @param billId
     * @param amount
     * @throws MalletException
     */
    @Override
    public void refund(Integer userId, String billId, Double amount) throws MalletException {
        User user = userMapper.selectRemainder(userId);
        if (user == null) {
            //抛出用户不存在异常
            throw new MalletException(ExceptionEnum.NO_USER);
        }

        String selectBillId = billMapper.selectBillId(billId);
        if (selectBillId==null){
            //抛出退款订单不存在异常
            throw new MalletException(ExceptionEnum.NO_BILL);
        }

        //得到退款后的剩余余额
        Double newRemainder = user.getRemainder() + amount;

        //更新余额
        userMapper.consume(userId, newRemainder);

        //插入订单列表

        //装配订单参数
        Bill bill = new Bill();
        bill.setUserId(userId);
        bill.setBillId(billId);
        bill.setRemainder(newRemainder);
        bill.setDetails(amount);
        bill.setRemark("退款用户");
        bill.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        //插入账单
        billMapper.insertConsume(bill);
    }
}
